<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/14 15:44
 */

namespace App\Http\Controllers;

use App\Http\Models\UserIntegralLog;
use App\Http\Models\DescribeRule;
use Auth;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class IntegralController extends Controller
{
    /**
     * 获取用户积分日志
     * @return string
     */
    public function getUserIntegralLog()
    {
        $userId = Auth::id();
        $list = Cache::remember('UserIntegralLog:'.$userId, 5, function () use ($userId) {
            return UserIntegralLog::leftjoin('goods', 'goods.id', '=', 'users_integral_log.good_id')
                ->select('users_integral_log.id', 'users_integral_log.user_id', 'users_integral_log.content', 'users_integral_log.status', 'users_integral_log.number', 'users_integral_log.created_at', 'goods.coverimage', 'goods.goodstitle')
                ->where('users_integral_log.user_id', $userId)
                ->orderBy('created_at', 'desc')
                ->paginate(env('LIMIT'));
        });
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list, 'user_id' => $userId]);
    }

    /**
     * 获取扣除积分的规则
     */
    public function getIntegralRule()
    {
        $list = Cache::remember('IntegralRule', 60, function () {
            return DescribeRule::select('content')->where(['type' => 1])->get();
        });
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 获取单个用户的积分轨迹
     * @return string
     */
    public function getIntegralTrajectory()
    {
        //Cache::forget('UserIntegralTrajectory:' . Auth::id());
        $allDate = Cache::remember('UserIntegralTrajectory:' . Auth::id(), 2, function () {
            $BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
            $endDate = date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));
            $allDate = [];
            $endDateD = explode('-', $endDate);
            $endTime = $endDateD[2] <= date('d') ? $endDateD[2] : date('d');
            for ($i = 1; $i <= $endTime; $i++) {
                $allDate[$i]['date'] = $endDateD[0] . '-' . $endDateD[1] . '-' . (($i >= 10) ? $i : '0' . $i);
                $allDate[$i]['integralNumber'] = 0;
            }
            $userId = Auth::id();
            $userIntegrel = UserIntegralLog::where('user_id', $userId)
                ->where('created_at', '<=', date('Y-m-d') . ' 23:59:59')
                ->where('created_at', '>=', $allDate[1]['date'] . ' 00:00:00')
                ->orderBy('created_at', 'asc')
                // ->where(['status' => 0])
                ->get();
            foreach ($allDate as $key => $va) {
                $number = 0;
                foreach ($userIntegrel as $kk => $value) {
                    if (strtotime(substr($value->created_at, 0, 10)) <= strtotime($va['date'])) {
                        if ($value->status == 0) {
                            $number -= $value->number;
                        } else {
                            $number += $value->number;
                        }
                    }
                    $allDate[$key]['integralNumber'] = $number;
                }

            }
            return $allDate;
        });
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => array_values($allDate)]);
    }
}